<template>
  <div>
    <el-card class="login-form-layout">
      <el-form aria-autocomplete="on" :model="loginForm" ref="loginForm" label-position="left">
        <h2 class="login-title color-main">用户注册</h2>

        <el-form-item prop="username">
          <el-input name="username" type="text" v-model="loginForm.username" autocomplete="on" placeholder="请输入账号" autofocus>
            <span slot="prefix">
              <svg-icon iconClass="person" class="color-main"></svg-icon>
            </span>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input name="password" :type="pwdType" v-model="loginForm.password" autocomplete="on" placeholder="请输入密码" show-password>
            <span slot="prefix">
              <!--<svg-icon icon-class="send" class="color-main"></svg-icon>-->
              <span class="el-icon-key"></span>
            </span>
          </el-input>
        </el-form-item>
        <el-form-item prop="rePassword">
          <el-input name="rePassword" :type="pwdType" v-model="loginForm.rePassword" autocomplete="on" placeholder="请再次确认密码">
            <span slot="prefix">
              <span class="el-icon-key"></span>
            </span> <span slot="suffix" @click="showPwd">
              <svg-icon icon-class="eye" class="color-main"></svg-icon>
            </span>
          </el-input>
        </el-form-item>
        <el-form-item prop="name">
          <el-input name="name" type="text" v-model="loginForm.name" autocomplete="on" placeholder="请输入昵称" >
            <span slot="prefix">
              <span class="el-icon-s-custom"></span>
            </span>
          </el-input>
        </el-form-item>
        <el-form-item prop="sex">
          <el-radio-group v-model="loginForm.sex">
            <el-radio v-model="loginForm.sex" border size="medium" label="0">男性</el-radio> border size="medium"
            <el-radio v-model="loginForm.sex" border size="medium" label="1">女性</el-radio>
            <el-radio v-model="loginForm.sex" border size="medium" label="2">保密</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="phone">
          <el-input name="phone" type="text" v-model="loginForm.phone" autocomplete="on" placeholder="请输入电话">
            <span slot="prefix">
              <span class="el-icon-phone"></span>
            </span>
          </el-input>
        </el-form-item>
        <el-form-item prop="email">
          <el-input name="email" type="text" v-model="loginForm.email" autocomplete="on" placeholder="请输入邮箱" >
            <span slot="prefix">
              <span class="el-icon-s-promotion"></span>
            </span>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button style="width: 100%" type="primary" :loading="loading" @click.native.prevent="handleRegister">注册</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>

export default {
  name: "register",
  data() {
    return {
      loginForm: {
        id: '',
        username: "",
        password: "",
        rePassword: "",
        sex: '2',
        name: '',
        phone: '',
        email: '',
      },
      loading: false,
      pwdType: "password",
    };
  },
  methods: {
    showPwd() {
      if (this.pwdType === "password") {
        this.pwdType = "";
      } else {
        this.pwdType = "password"
      }
    },
    handleRegister() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          this.$store
              // 有命名空间
              .dispatch("user/register", this.loginForm)
              .then(response => {
                debugger
                this.loading = false;
                let data = response.data;
                if (data.code === '0') {
                  this.$router.push({
                    path: "/login",
                    query: {
                      data: response.data
                    }
                  })
                } else {
                  this.$router.push({
                    path: "/error",
                    query: {
                      message: response.data
                    }
                  })
                }
              })
              .catch(() => {
                this.loading = false;
              })
        } else {
          console.error("参数验证不合法！")
          return false;
        }
      })
    },
  }
}
</script>

<style scoped>
.login-form-layout {
  position: absolute;
  left: 0;
  right: 0;
  width: 460px;
  margin: 140px auto;
  border-top: 10px solid #409eff;
}

.login-title {
  text-align: center;
}

.login-center-layout {
  background: #409eff;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  margin-top: 200px;
}
</style>